package com.AI.AIDemo.Demos.Web.Config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/*
 * @Author:大力王
 * @Date:2025/4/24-24-22:35
 * 开始了
 */
@Configuration
public class SwaggerConfig {
    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GroupedOpenApi globalOpenApi() {
        String[] paths = { "/**" };
        String[] packagedToMatch = { "com.AI.AIDemo.Demos.Web.Controller.FileController" };
        return GroupedOpenApi.builder().group("文件模块")
                .pathsToMatch(paths)
                .addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(new HeaderParameter().name("groupCode").example("测试").description("集团code").schema(new StringSchema()._default("BR").name("groupCode").description("集团code"))))
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public GroupedOpenApi globalOpenApi2() {
        String[] paths = { "/**" };
        String[] packagedToMatch = { "com.AI.AIDemo.Demos.Web.Controller.LoginController" };
        return GroupedOpenApi.builder().group("登录模块")
                .pathsToMatch(paths)
                .addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(new HeaderParameter().name("groupCode").example("测试").description("集团code").schema(new StringSchema()._default("BR").name("groupCode").description("集团code"))))
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public GroupedOpenApi globalOpenApi3() {
        String[] paths = { "/**" };
        String[] packagedToMatch = { "com.AI.AIDemo.Demos.Web.Controller.AIController" };
        return GroupedOpenApi.builder().group("AI测试模块")
                .pathsToMatch(paths)
                .addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(new HeaderParameter().name("groupCode").example(111).description("集团code").schema(new StringSchema()._default("BR").name("groupCode").description("集团code"))))
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
                .info(new Info()
                        .title("AI智能系统API")
                        .version("1.0")
                        .description( "这是我在学习AI做的项目")
                        .termsOfService("http://doc.xiaominfo.com")
                        .license(new License().name("Apache 2.0")
                                .url("http://doc.xiaominfo.com"))
                )
                .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components()
                        .addSecuritySchemes(HttpHeaders.AUTHORIZATION,new SecurityScheme()
                                .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP).scheme("bearer")));
    }
//    @Bean
//    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
//        return openApi -> {
//            if (openApi.getTags()!=null){
//                openApi.getTags().forEach(tag -> {
//                    Map<String,Object> map=new HashMap<>();
//                    map.put("x-order", RandomUtil.randomInt(0,100));
//                    tag.setExtensions(map);
//                });
//            }
//            if(openApi.getPaths()!=null){
//                openApi.addExtension("x-test123","333");
//                openApi.getPaths().addExtension("x-abb",RandomUtil.randomInt(1,100));
//            }
//
//        };
//    }
}
